/**
 * Created with IntelliJ IDEA.
 * User: "Balasubramanian"
 * Date: 8/15/13
 * Time: 1:58 PM
 * To change this template use File | Settings | File Templates.
 */

package com.twu.biblioteca;

import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.logging.Logger;

public class MoviesLibrary
{

    HashMap<String,Movie> movies=null;
    Logger logger;
    private String SOURCE_FILE="";
    String SEPARATOR=" ";

    //constructor
    MoviesLibrary(String sourceFilePath,Logger logger)
    {
        this.logger=logger;
        SOURCE_FILE=sourceFilePath;

        movies=new HashMap<>();
        readMoviesFromFile();

    }

    ArrayList<Movie> getAllMovies()
    {
        ArrayList<Movie> movieList=new ArrayList<>();

        for(Map.Entry<String,Movie> movie:movies.entrySet())
        {
            movieList.add(movie.getValue());
        }

        return movieList;
    }

    private void readMoviesFromFile()
    {
        ArrayList<String> linesFromFile=UtilityFunctions.readAllLinesFromFile(SOURCE_FILE,logger);

        for(String input:linesFromFile)
        {
            String[] fields=input.split(SEPARATOR);
            Movie movie=new Movie(fields[0].toUpperCase(),Integer.parseInt(fields[1]),fields[2],Integer.parseInt(fields[3]));
            movies.put(fields[0].toUpperCase(),movie);
        }
    }
}